using System;
using System.Windows.Input;

namespace Wen.Core {

	public sealed class SimpleCommand<T> : ICommand where T : class
	{
		private readonly Action<T> _executeAction;
		private readonly Func<T, bool> _canExecutePredicate;

		public void Execute(T param) {
			if (CanExecute(param))
				_executeAction(param);
		}

		void ICommand.Execute(object parameter) {
			Execute(parameter as T);
		}

		public bool CanExecute(T param) {
			return _canExecutePredicate == null || _canExecutePredicate(param);
		}

		bool ICommand.CanExecute(object parameter) {
			return CanExecute(parameter as T);
		}

		public event EventHandler CanExecuteChanged {
			add { CommandManager.RequerySuggested += value; }
			remove { CommandManager.RequerySuggested -= value; }
		}

		public SimpleCommand(Action<T> execute, Func<T, bool> canExecute = null) {
			if (execute == null)
				throw new ArgumentNullException("execute");
			_executeAction = execute;
			_canExecutePredicate = canExecute;
		}
	}

}